import 'dart:io';

import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/OssUpload.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

///用户认证
class UserAuthController extends BaseController {
  int gender = 0;

  // 人面
  AssetEntity? idcardFront;

  String idcardFrontUrl = '';

  // 国徽
  AssetEntity? idcardOpposite;

  String idcardOppositeUrl = '';

  ///已上传的图片地址
  List<String>? imageUrlList = [];

  ///购买者姓名
  final TextEditingController nameTxtCtr = TextEditingController();

  ///身份证号
  final TextEditingController idcardTxtCtr = TextEditingController();

  String pageTitle = '上传身份证';

  ///性别
  int sexSelected = 0;

  ///是否同意协议
  bool isAgree = false;

  ///实名认证的结果
  Map<String, dynamic> data = {};

  @override
  void onInit() {
    getAuthInfo();
    super.onInit();
  }

  ///获取实名认证的数据
  void getAuthInfo() {
    Tools.showLoading();
    Network.getUserAuth().then((value) async {
      Tools.hideLoading();
      print("实名认证结果：$value");
      data = value;
      pageTitle = value['trueName'] != null ? '实名认证已完成' : '实名认证';
      nameTxtCtr.text = value['trueName'] ?? '';
      idcardTxtCtr.text = value['idcard'] ?? '';
      idcardFrontUrl = value['idcardFront'];
      idcardOppositeUrl = value['idcardOpposite'];
      gender = value['gender'];
      sexSelected = gender;
      update();
    });
  }

  Future<void> submit() async {
    if (isAgree == false) {
      Tools.showToast("请先同意授权");
      return;
    }

    // 正反面都传/ 都不传
    bool isIdCardValid = (idcardFront != null && idcardOpposite != null) || (idcardFront == null && idcardOpposite == null);
    if (!isIdCardValid) {
      Tools.showToast("请上传完整的身份证（正反面都上传）");
      return;
    }

    if (nameTxtCtr.text.isEmpty) {
      Tools.showToast("请输入姓名");
      return;
    }

    if (idcardTxtCtr.text.isEmpty) {
      Tools.showToast("请输入身份证号码");
      return;
    }

    Tools.showLoading();
    //文件上传

    List tempImageList = [idcardFront, idcardOpposite];
    if (tempImageList.isNotEmpty) {
      List<File> fileList = [];
      tempImageList.forEach((element) async {
        File? file = await element.file;
        fileList.add(file!);
      });
      imageUrlList = await OssUpload.upoladFiles(fileList);
    }

    //这里请求接口
    Map<String, dynamic> params = {};
    params['gender'] = sexSelected;
    params['idcard'] = idcardTxtCtr.text;

    if (imageUrlList != null && imageUrlList!.length == 2) {
      params['idcardFront'] = imageUrlList!.first;
      params['idcardOpposite'] = imageUrlList!.last;
    }

    params['trueName'] = nameTxtCtr.text;

    Network.userAuth(params).then((value) {
      print("实名认证返回结果:$value");
      if (value == true) {
        Tools.showToast("认证完成");
        getAuthInfo();
        Tools.hideLoading();
      }
    }).catchError((onError) {
      Tools.hideLoading();
    });
  }
}
